using System;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Collections.Generic;
using System.Text;

namespace Weazel.Toad.Application.Controls
{
  public static class GraphicsLoader
  {
    /// <summary>
    /// Loads an image stored in the same namespace as the type argument
    /// with the specified name
    /// </summary>
    /// <param name="type">Type in namespace to load from</param>
    /// <param name="name">name of image (ressource)</param>
    /// <returns>Image</returns>
    public static Image LoadImage(Type type, string name)
    {
      // get a reference to the assembly (maybe different from the present one)
      Assembly assembly = Assembly.GetCallingAssembly();

      // get the name space of the calling class 
      int lastDelimiter = type.FullName.LastIndexOf('.');
      string location = type.FullName.Substring(0, lastDelimiter);

      // get a stream ..
      Stream imageStream =
        assembly.GetManifestResourceStream(location + "." + name);

      // create and return the image
      Image image = Image.FromStream(imageStream);

      // do NOT close the stream -> will cause the image to be invalidated

      return image;
    }
  }
}
